package com.kitty.game.task.service.targetHandler;

import com.kitty.game.gm.GmCommandHandler;
import com.kitty.game.pet.model.Pet;
import com.kitty.game.role.model.Role;
import com.kitty.game.config.TaskSet;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.task.model.product.targetparam.FetchPetTargetParam;
import com.kitty.game.task.model.product.TaskTarget;
import com.kitty.game.task.model.product.TaskTargetType;
import com.kitty.game.task.service.NewTaskService;
import com.kitty.game.utils.NotifyModules;
import com.kitty.listener.BaseGameEvent;
import com.kitty.game.enter.RespGeneralNotify;
import com.kitty.mina.message.MessagePusher;
import com.kitty.game.pet.service.PetService;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class FetchPetTargetHandler extends TaskTargetHandler {

    /**处理事件*/
    @Override
    public void handleEvent(Role role, BaseGameEvent baseGameEvent) {
        /**判断当前进行的任务是否有领取宠物，有则进行领取操作*/

        NewTaskService taskService = SpringUtils.getBean(NewTaskService.class);
        List<TaskSet> taskSetList = taskService.getAcceptedTasksMatchTarget(role, TaskTargetType.FETCH_PET);
        if (taskSetList.isEmpty()) {return ;}

        TaskSet taskSet = taskSetList.get(0);
        /**没有正在进行领取宠物的任务时，返回*/
        if (taskSet == null) {return ;}

        TaskTarget taskTarget = taskService.getRoleDoingTaskTarget(role, taskSet);
        /**任务目标都完成时，可能获得null*/
        if (taskTarget == null) {return ;}
        FetchPetTargetParam fetchPetTargetParam = (FetchPetTargetParam)taskTarget.getTaskTargetParam();

        PetService petService = SpringUtils.getBean(PetService.class);
        /**原来代码是按角色等级给宠物等级的，暂时也这样处理*/
        Pet pet;
        if(taskSet.getId() == 51){
             pet = petService.addPet(role, fetchPetTargetParam.getName(), role.getLevel(), false);
        }else{
             pet = petService.addPet(role, fetchPetTargetParam.getName(), role.getLevel(), true);
        }
        //因为鬼宠原因导致赠送的宠物不能参战
        //设置一下参战宠物
        if(taskSet.getId() != 51){
            role.getPetBox().setFightPetId(pet.getId());
            role.getPetBox().setFightYangPetId(pet.getId());
        }
        /**更新任务进度*/
        taskService.updateTargetProgress(role, taskSet, taskTarget);
        if (taskSet.getId() == 51){
            SpringUtils.getBean(GmCommandHandler.class).level(role,75);
        }
    }

    @Override
    public void triggerNow(Role role, TaskSet taskSet, TaskTarget taskTarget) {
        /**触发任务领取宠物目标*/
        FetchPetTargetParam fetchPetTargetParam = ((FetchPetTargetParam)taskTarget.getTaskTargetParam());

        // 类似格式
        //#I宠物|松鼠(宝宝)$3%bind#I
        //#I宠物|海龟(宝宝)$23%bind=2592000#I
        StringBuilder petInfo = new StringBuilder();
        petInfo.append("#I宠物|").append(fetchPetTargetParam.getName())
                .append("(神兽)$").append(fetchPetTargetParam.getLevel());
        if (fetchPetTargetParam.getBind() > 0) {
            petInfo.append("bind=").append(fetchPetTargetParam.getBind());
        } else if (fetchPetTargetParam.getBind() < 0) {
            petInfo.append("bind");
        }
        petInfo.append("#I");

        /**弹出领取宠物*/
        RespGeneralNotify respGeneralNotify = new RespGeneralNotify();
        respGeneralNotify.setNotify((short) NotifyModules.NOTICE_FETCH_BONUS);//领取宠物
        respGeneralNotify.setValue(petInfo.toString());
        MessagePusher.pushMessage(role, respGeneralNotify);
    }
}
